package com.xiaoshuidi.cloud.module.report.dal.mapper.report;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.xiaoshuidi.cloud.framework.mybatis.core.mapper.BaseMapperX;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.IncomeDetailDayReportDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
@InterceptorIgnore(tenantLine = "true")
public interface IncomeDetailDayReportMapper extends BaseMapperX<IncomeDetailDayReportDO> {


    @Select(" select count(1) from (select count(id) " +
            " from report_income_detail_day " +
            " where statistics_month = #{currentMonth} group by contract_id) as temp")
    long countDetailsInCurrentMonth(@Param("currentMonth")String currentMonth);

    @Select("select statistics_month,contract_id " +
            " from report_income_detail_day " +
            " where statistics_month = #{currentMonth} group by contract_id")
    List<IncomeDetailDayReportDO> selectDetailsInCurrentMonth(@Param("currentMonth")String currentMonth);
    @Select("select * " +
            " from report_income_detail_day " +
            " where statistics_month = #{currentMonth} " +
            " group by id,contract_id order by contract_id asc limit #{from}, #{pageSize}")
    List<IncomeDetailDayReportDO> selectDetailsInCurrentMonthInTurn(@Param("currentMonth")String currentMonth, @Param("from")Integer from,@Param("pageSize")Integer pageSize);

    /**
     * 跟换合同id查询最近10条数据
     * @param contractId 合同id
     * @return
     */
    @Select("select * from report_income_detail_day where contract_id =#{contractId} order by id desc limit 10")
    List<IncomeDetailDayReportDO> selectByContractId(@Param("contractId")Long contractId);

    @Select("select * " +
            " from report_income_detail_day " +
            " where statistics_month = #{currentMonth} and contract_id = #{contractId} order by id desc")
    List<IncomeDetailDayReportDO> selectDetailsInCurrentMonthByContractId(@Param("currentMonth")String currentMonth,@Param("contractId")Long contractId);

    @Select("select * " +
            " from report_income_detail_day " +
            " where contract_id = #{contractId} order by id desc limit 1")
    IncomeDetailDayReportDO getRecentIncomeReport(@Param("contractId")Long contractId);
 }
